package com.qianfeng.teachdemo.ui;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.adapter.LeftAdapter;
import com.qianfeng.teachdemo.bean.MenuIconMenu;
import com.qianfeng.teachdemo.utils.AppManager;

import java.util.ArrayList;


/**
 * @version v1.0
 * @类描述：
 * @项目名称：VolleyaDemo
 * @包 名： com.qianfeng.volleyademo.ui
 * @类名称：BaseActivity
 * @创建人：张唯
 * @创建时间：14-9-30
 * @修改人：
 * @修改时间：
 * @修改备注：
 */
public class BaseActivity extends ActionBarActivity implements View.OnClickListener {
    public Activity activity;
    public SlidingMenu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        AppManager.getAppManager().addActivity(this);
    }
    protected void initSlidingMenu() {
        menu = new SlidingMenu(this);// 实例化
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        menu.setMode(SlidingMenu.LEFT);
        menu.setShadowWidthRes(R.dimen.shadow_width);// 设置阴影的宽度
        menu.setBehindOffset(getWindowManager().getDefaultDisplay().getWidth() / 3);// 设置菜单滑出是主界面的宽度
        menu.setFadeEnabled(true);// 设置渐变效果
        menu.setFadeDegree(0.35f);// 设置渐变值
        // 设置左边的menu布局
        menu.setMenu(createLeftMenuView());
        // menu.setSecondaryMenu(creatRightMenuView());// 设置右边的布局
        menu.attachToActivity(activity, SlidingMenu.SLIDING_WINDOW);
        menu.showContent();
    }

    /**
     *
     * @Title: createLeftMenuView
     * @说 明:创建menu菜单界面
     * @参 数: @return
     * @return View 返回类型
     * @throws
     */
    private View createLeftMenuView() {
        ArrayList<String> list = new ArrayList<String>();
        String[] contents = getResources().getStringArray(R.array.menu_left_array);
        for (int i = 0; i < contents.length; i++) {
            list.add(contents[i]);
        }
        View view = getLayoutInflater().inflate(R.layout.menu_left_layout, null);
        TextView menuTitle = (TextView) view.findViewById(R.id.menu_title);
        menuTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, getAactionBarHeight()));
        ListView listView = (ListView) view.findViewById(R.id.menu_left_list);
        listView.setOnItemClickListener(new leftMenuOnitemClick(list));
        listView.setAdapter(new LeftAdapter(this,MenuIconMenu.getMenuList()));
        return view;
    }

    private class leftMenuOnitemClick implements AdapterView.OnItemClickListener {
        private ArrayList<String> list;

        public leftMenuOnitemClick(ArrayList<String> list) {
            super();
            this.list = list;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 1:
                    Intent intent = new Intent(activity,LoginActivity.class);
                    startActivity(intent);
                    break;

            }
        }
    }

    private int getAactionBarHeight() {
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
        }
        return actionBarHeight;
    }



    @Override
    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.menu_right_icon:
//                break;
//            case R.id.menu_right_loginBtn:
//                startActivity(new Intent(this, LoginActivity.class));
//                break;
//        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        AppManager.getAppManager().finishActivity(this);
    }
}
